﻿using August.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using August.Core;
using System.IO;
using BatteryShapeGenerator;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        //原始图片
        Image originImage;
        //裁切的模板图片
        Image templateImage = null;
        //创建的模板句柄
        IntPtr intPtr = IntPtr.Zero;
        //所有需要检测的图片路径
        string[] files;
        public Form1()
        {
            InitializeComponent();
            //files = Directory.GetFiles(@"C:\Users\yaoshun\Desktop\sample");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            HC.Instance.Imread(@"C:\Users\yaoshun\Desktop\sample\1.png", out originImage);
            operationPanel1.Dev_DispImage(originImage);
        }
        August.UI.Rectancle rect;
        private void button2_Click(object sender, EventArgs e)
        {
            operationPanel1.Dev_SetWidth(3);
            operationPanel1.Dev_SetColor(Color.Red);
            //默认生成一个位置为100，100长宽为100的矩形
            operationPanel1.Dev_GenRectancleXld(100, 100, 100, 100, 0, out rect);
            rect.IsUserShape = true;
            operationPanel1.Dev_DispXld(rect);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            HC.Instance.CopyRectancleROI(originImage, out templateImage, new Rectangle((int)rect.PointFs[1].X, (int)rect.PointFs[1].Y, (int)rect.Width, (int)rect.Height));
            operationPanel2.Dev_DispClear();
            operationPanel2.Dev_DispImage(templateImage);
            operationPanel2.Dev_AutoFit();
            Console.WriteLine($"绘制的点为{(int)rect.PointFs[0].X},{(int)rect.PointFs[0].Y}");
        }



        private void button4_Click(object sender, EventArgs e)
        {
            //学习裁切图片的模板存储到 模板句柄 
            HC.Instance.CreateShapeModel(templateImage, "auto", -0.39, 0.79, "auto", "auto", "use_polarity", "auto", "auto", out intPtr);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            ImageProcess();
        }

        int index = 0;
        private void button6_Click(object sender, EventArgs e)
        {
            HC.Instance.Imread(files[index], out originImage);
            ImageProcess();
            index++;
            if (index >= files.Length)
            {
                index = 0;
            }
        }


        private void ImageProcess()
        {
            bool ret = HC.Instance.FindShapeModel(originImage, intPtr, -0.39, 0.79, 0.5, 1, 0.5, "least_squares", 0, 0.9, out double row, out double col, out double hv_Angle, out double hv_Score);
            Console.WriteLine($"找到的点为{col},{row}");
            Rectancle rectancle;
            operationPanel1.Dev_GenRectancleXld((float)col, (float)row, rect.Width, rect.Height, (float)hv_Angle, out rectancle);
            operationPanel1.Dev_DispClear();
            operationPanel1.Dev_DispImage(originImage);
            operationPanel1.Dev_SetColor(Color.LimeGreen);
            operationPanel1.Dev_DispXld(rectancle);
            operationPanel1.Dev_AutoFit();
        }

        private void button7_Click(object sender, EventArgs e)
        {

            NormalBatteryTap normalBatteryTap = new NormalBatteryTap();
            normalBatteryTap.ForeLineLenth = 0;
            normalBatteryTap.BehindLineLength = 60000;
            normalBatteryTap.TopRadius = 3;
            normalBatteryTap.BottomRadius = 3;
            normalBatteryTap.Width = 40;
            normalBatteryTap.Height = 40;
            normalBatteryTap.MarkHeight = 0;
            normalBatteryTap.MarkRadius = 0;
            normalBatteryTap.MarkWidth = 0;
            normalBatteryTap.Angle = 89.99f;
            BatteryTabShape batteryTabShape = new BatteryTabShape(normalBatteryTap);
            var points = batteryTabShape.GetPoints(0.1F, true, true);
            operationPanel1.CoordinatePoint(points.ToArray());

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            operationPanel1.ShapeSelected += OperationPanel1_ShapeSelected;
        }

        private void OperationPanel1_ShapeSelected(object sender, ShapeBase e)
        {
            Cross cross = e as Cross;
            Console.WriteLine("选中点INdex " +  cross.SelectedIndex);
        }
    }
}
